<template>
  <li>
    <p class="title">
      <span>{{ num }}.</span>{{ title }}
    </p>
    <p v-for="i in keyNum" :key="i">
      答案：<el-input v-model="answer[i - 1]" class="answer" :disabled="type == 'readonly'"></el-input>
    </p>
    <div v-if="type == 'readonly'" style="background-color: #f3f3f3;padding: 20px">
      <p>正确答案：{{data.trueAnswer}}</p>
      <p>你的答案：{{data.userAnswer}}</p>
      <p>描述：{{data.expound}}</p>
    </div>
    <el-divider></el-divider>
  </li>
</template>

<script>
export default {
  props: ["data", "num", "lastAnswer"],
  data() {
    return {
      // 将刷新前的数据进行赋值
      answer: [],
      type: this.$route.query.type,

    };
  },
  created () {
    if (this.lastAnswer == undefined || this.lastAnswer == null) {
      return;
    }
    this.answer = this.lastAnswer.split("#");
  },
  watch: {
      answer: function(val) {
        let tempArr = JSON.parse(JSON.stringify(val))
        let element = '';
        for (let i = 0; i < tempArr.length; i++) {
          if (tempArr[i] == null || tempArr == undefined) {
            tempArr[i] = '';
          }
          if (i != 0) {
            element += '#' + tempArr[i]
          } else {
            element += tempArr[i]
          }
        }
        const temp = {
          index: Number(this.num) - 1,
          data: element,
        }
        this.$store.commit("saveAnswerCloze", temp);
      }
    },
  computed: {
    title() {
      return this.data.title.split("###")[0];
    },
    keyNum() {
      return Number(this.data.title.split("###")[1]);
    },
  },
};
</script>

<style lang="scss" scoped>
li {
  list-style: none;
  .title {
    span {
      color: #63b1fe;
    }
  }
  p {
    padding-left: 15px;
  }
  .answer {
    width: 300px;
  }
}
</style>
